package graphql

import (
	"context"
	"log"
	"regexp"
)

type SWVersion string

const (
	SW8 SWVersion = "sw8"
	SW9 SWVersion = "sw9"
)

var SW9Regex = regexp.MustCompile(`^9.\d+.\d+`)

func CheckSWVersion() SWVersion {
	var query struct {
		Version string `graphql:"version"`
	}
	err := DefaultClient.Query(context.Background(), &query, nil)
	if err != nil {
		// ignore and use 8.x.x
		log.Printf("check sw version failed, err: %s", err)
		return SW8
	}

	if SW9Regex.Match([]byte(query.Version)) {
		return SW9
	}

	return SW8
}
